package com.refactor.enums;

import com.refactor.impl.ChildrensHander;
import com.refactor.impl.NewReleseHander;
import com.refactor.impl.PointHandler;
import com.refactor.impl.RegularHander;

import java.lang.reflect.Constructor;

/**
 * 影片分类规则
 */
public enum MovieTypes {

    REGULAR(0, RegularHander.class),
    NEW_RELEASE(1, NewReleseHander.class),
    CHILDRENS(2, ChildrensHander.class);

    private Integer type;
    private Class<? extends PointHandler> entityClass;

    public Integer getType() {
        return type;
    }

    MovieTypes(Integer type, Class<? extends PointHandler> entityClass) {
        this.type = type;
        this.entityClass = entityClass;
    }
    public PointHandler instance(int type){
        try {
            Constructor<? extends PointHandler> declaredConstructor = entityClass.getDeclaredConstructor(Integer.class);
            declaredConstructor.setAccessible(true);
            PointHandler instance = declaredConstructor.newInstance(type);
            return instance;
        } catch (Exception e){
            System.out.println("instance error");
        }
        return null;
    }
    public static MovieTypes parse(Integer type){
        for (MovieTypes sourceTypeEnum : values()){
            if (sourceTypeEnum.type==type){
                return sourceTypeEnum;
            }
        }
        return null;
    }
}
